package guitforl.view

import guitforl.builders.TabBuilder
import xml.{XML, NodeSeq}
import guitforl.parsers.xml.ImplementationReader
import javax.swing._
import plaf.metal.{OceanTheme, MetalLookAndFeel, DefaultMetalTheme}
import org.heresylabs.jlafchooser.JLAFChooser
import java.awt.event.{KeyEvent, ActionEvent, ActionListener}
import java.awt.{Toolkit, Dimension}
import guitforl.wrappers.{LayoutFactory, TabWrapper}

class Form extends JFrame {
  setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)

  val templatesFolder: String = "./templates/"
  val realization: NodeSeq = XML.loadFile(templatesFolder + "implementation.xml")

  val tabs = new JTabbedPane
  add(tabs)

  prepareSize(0.7, 0.7)
  buildTabs
  buildMenu

  pack
  setVisible(true)

  UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  SwingUtilities.updateComponentTreeUI(this);

  def buildTabs(): Unit = {
    for{tabBlock <- (realization \ "tab")} {
      val template = XML.loadFile(templatesFolder + (tabBlock \ "@template") + ".xml")
      val wrapTab: TabWrapper = new TabWrapper((tabBlock \ "@name").text)

      tabs.add((tabBlock \ "@name").text, wrapTab)

      TabBuilder.buildTab(wrapTab, template)
      ImplementationReader.read(tabBlock, wrapTab)
    }
  }

  def buildMenu(): Unit = {
    val thisMenuBar = new JMenuBar();

    val fileMenu = new JMenu("Theme");
    val configMenu = new JMenu("Congifuration");

    val themeMenuItem = new JMenuItem("Theme chooser", KeyEvent.VK_N);
    val configMenuDebug = new JMenuItem("Enable Debug")
    val aboutItem = new JMenuItem("About..")

    val rootComponent = this

    val action = new ActionListener() {
      def actionPerformed(actionEvent: ActionEvent) {
        UIManager.setLookAndFeel(JLAFChooser.showDialogLAF(tabs, "Chooser"))
        SwingUtilities.updateComponentTreeUI(rootComponent);
      }
    }

    val changeDebugOption = new ActionListener() {
      def actionPerformed(actionEvent: ActionEvent) {
        tabs.removeAll
        if (configMenuDebug.getText.equals("Enable Debug")) {
	        LayoutFactory.debug = true
          buildTabs
          configMenuDebug.setText("Disable Debug")
        } else {
	        LayoutFactory.debug = false
          buildTabs
          configMenuDebug.setText("Enable Debug")
        }
      }
    }

    val aboutAction = new ActionListener() {
      def actionPerformed(actionEvent: ActionEvent) {
        JOptionPane.showMessageDialog(getParent, "GUI Templetor for Laboratory\nA. Kozlov (masterzim.art@gmail.com)\nR.Yavorsky (planime666@gmail.com)");
      }
    }

    themeMenuItem.addActionListener(action)
    fileMenu.add(themeMenuItem)

    configMenuDebug.addActionListener(changeDebugOption)
    aboutItem.addActionListener(aboutAction)
    configMenu.add(configMenuDebug)
    configMenu.add(aboutItem)

    thisMenuBar.add(fileMenu)
    thisMenuBar.add(configMenu)
    setJMenuBar(thisMenuBar)
  }

  def prepareSize(xCoef:Double, yCoef:Double): Unit = {
    val screenSize:Dimension = Toolkit.getDefaultToolkit().getScreenSize();
    setPreferredSize(new Dimension((screenSize.width * xCoef).toInt, (screenSize.height * yCoef).toInt))
    setLocation(((screenSize.width - (screenSize.width * xCoef)) / 2).toInt, ((screenSize.height - (screenSize.height * yCoef)) / 2).toInt)
  }

}
